package sebjul.ecom.servlet;

import java.util.*;

import javax.ejb.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

import sebjul.ecom.dao.*;
import sebjul.ecom.entities.*;

@WebServlet("/CatalogueByGenre")
public class CatalogueByGenre extends HttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	GenreDao genreDao;
	@EJB
	ArticleDao articleDao;

    public CatalogueByGenre() {
        super();
    }


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		
		
		
		int nbrpage=2;
		int idpage = 1;
		String stidpage=request.getParameter("idpage");
		if (stidpage != null){idpage = Integer.parseInt(request.getParameter("idpage"));}
		int numpage = ((idpage-1)*(nbrpage)); 
		int genreint = Integer.parseInt(request.getParameter("genre"));
		
			
			
		
		List<Article> articles = articleDao.findArticleGenre(genreint,numpage,nbrpage);
		request.setAttribute("articles", articles);
		int pagination= articleDao.countArticleGenre(genreint);
			
			float nombrepage=pagination/nbrpage;
			float reste= pagination % nbrpage; 
			int pageplus=0;
			if (reste>0){pageplus=1;}
			pagination=((int) (nombrepage))+pageplus;
			
		request.setAttribute("origine", "CatalogueByGenre?genre="+genreint+"&");
		request.setAttribute("idpage", idpage);
		request.setAttribute("pagination", pagination);
		RequestDispatcher rd = request.getRequestDispatcher("catalogue.jsp");
		rd.forward(request, response);
		
		
		
		
	}


	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
